import gtk

from ui import *
from soap import *

from ui.GladeBase import GladeBase
from utils import logger, envir, check_authorization, ADMIN, AUTHOR

NO_ENTITY = -1

class ManageBookstats(GladeBase):
    def __init__(self, author=None, top_component=None):
        logger.debug("UI: %s instance created" % self.__class__)
        GladeBase.__init__(self, 'manage_bookstats_frame', top_component)
        self.author = None
        self.genre = None
        
        # load genres
        genre_list = gtk.ListStore(int, str)
        try:
            for genre in client.get_genres():
                genre_list.append([genre.id, genre.name])
        except Exception as e:
            get_error_dialog(self.get_top_component(), e)
            
        self.genre_combobox.set_model(genre_list)
        
        author_list = gtk.ListStore(int, str, str)
        try:
            for author in client.get_authors(None):
                author_list.append([author.id, author.name, author.surname])
        except Exception as e:
            get_error_dialog(self.get_top_component(), e)
            
        self.author_combobox.set_model(author_list)
        
        if check_authorization(envir.get_user(), AUTHOR):
            self.author_combobox.set_sensitive(False)
            self.author = envir.get_user()['subject']
        
        # Setting up product list
        self._load_to_gui()
    
    def _init_gui_components(self):
        """
        Intialize gui components as needed. This is useful in the contructor or 
        binded with particular actions (for instance, a "clear" button, or "new" button) 
        """
        self.book_list.clear()

    def _retrieve_components(self):
        """
        Retrieve all used gui components from builder. This method is called in the
        GladeBase constructor
        """
        self.get_from_builder('book_treeview')
        self.get_from_builder('book_list')     
        self.get_from_builder('genre_combobox')
        self.get_from_builder('author_combobox')       
    
    def _load_to_gui(self):
        self._init_gui_components()
        
        try:
            for stat in client.get_stats(self.genre, self.author):
                row = [stat.id, stat.name, stat.edition, stat.prints, 
                       stat.price, stat.sold, stat.quantity]
                self.book_list.append(row)
        except Exception as e:
            get_error_dialog(self.get_top_component(), e)
            
    # Signals
    def on_search_button_clicked(self, object, data=None):
        iter = self.genre_combobox.get_active_iter()
        if iter:
            self.genre = self.genre_combobox.get_model().get_value(iter, 0)
        else:
            self.genre = None
        
        iter = self.author_combobox.get_active_iter()
        if check_authorization(envir.get_user(), ADMIN):
            if iter:
                self.author = self.author_combobox.get_model().get_value(iter, 0)
            else:
                self.author = None
            
        self._load_to_gui()
    
    def on_reset_button_clicked(self, object, data=None):
        self.author_combobox.set_active(-1)
        self.genre_combobox.set_active(-1)